package com.czl.module_storehouse.activity.remand.damage;

import android.os.Bundle;
import android.view.LayoutInflater;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.blankj.utilcode.util.SpanUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.czl.base.data.bean.tengyun.ProductBean;
import com.czl.module_base.activity.BaseActivity;
import com.czl.module_storehouse.R;
import com.czl.module_storehouse.databinding.ActivityDamageRecordProductBinding;


import com.czl.base.data.bean.tengyun.RecordRemandBean;
import com.czl.module_storehouse.event.DamageRecordEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xubin
 */
public class DamageRecordProductActivity extends BaseActivity<ActivityDamageRecordProductBinding> {
    private Adapter mAdapter;

    @Override
    protected ActivityDamageRecordProductBinding getBinding(LayoutInflater layoutInflater) {
        return ActivityDamageRecordProductBinding.inflate(layoutInflater);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        mToolBinding.toolbarContentTitle.setText("损坏物品");
        mAdapter = new Adapter(R.layout.item_damage_record_product, new ArrayList<>());
        binding.recyclerView.setAdapter(mAdapter);
        EventBus.getDefault().register(this);
    }

    @Subscribe(threadMode = ThreadMode.POSTING, sticky = true)
    public void onDataEvent(DamageRecordEvent event) {
        RecordRemandBean recordRemandBean = event.getRecordRemandBean();
        if (recordRemandBean == null) {
            return;
        }
        mAdapter.addData(recordRemandBean.getProductList());
    }

    static class Adapter extends BaseQuickAdapter<ProductBean, BaseViewHolder> {

        public Adapter(int layoutResId, @Nullable List<ProductBean> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(@NonNull BaseViewHolder helper, ProductBean item) {
            helper.setText(R.id.tv_code, "编号：" + item.getProductCode())
                    .setText(R.id.tv_model, "型号：" + item.getProductModel());
            SpanUtils.with(helper.getView(R.id.tv_title))
                    .append(item.getProductName() == null ? "" : item.getProductName())
                    .append(item.getDamageTypeStr(" 【", "】"))
                    .setForegroundColor(ContextCompat.getColor(getContext(), R.color.color_25A468))
                    .create();
        }
    }
}
